package com.test.controller;

import com.test.pojo.Article;
import com.test.pojo.ArticleType;
import com.test.service.ArticleService;
import com.test.service.ArticleTypeService;
import com.test.util.helper.MyPage;
import com.test.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class SystemRoleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping("/")
    public String getAll(@RequestParam(value = "newsState", required = false, defaultValue = "2")Byte newsState,
            @RequestParam(value = "newsTitle", required = false, defaultValue = "")String newsTitle,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
            Map<String, Object> map){
//        PageHelper.startPage(pageNum, pageSize);
//        MyPage<Article> items = articleService.getAll(newsState, newsTitle, pageNum, pageSize);
//        PageInfo<Article> pageInfo = new PageInfo<>(items);
//        map.put("pageInfo", pageInfo);
//        System.out.println("pageInfo Total = " + pageInfo.getTotal());
//        map.put("items", items);
        MyPage<Article> page = articleService.getAll(newsState, newsTitle, pageNum, pageSize);
        map.put("page", page);
        map.put("state", newsState);
        map.put("title", newsTitle);
        return "role/list";
    }

    @GetMapping("/toMain")
    public String toMain(){
        return "main/main";
    }

    @GetMapping("/toAdd")
    public String toAdd(Map<String, Object> map){
        List<ArticleType> items = articleTypeService.getAll();
        System.out.println("items.size() = " + items.size());
        map.put("items", items);
        return "role/add";
    }

    @GetMapping("/check-is-exist")
    @ResponseBody
    public boolean checkIsExist(String title){
        return articleService.selectOneByTitle(title) != null;
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseMessage add(@Validated Article article, BindingResult bindingResult,
                               @RequestParam(value = "file", required = false)MultipartFile multipartFile,
                               HttpServletRequest request) throws ParseException {
        if(bindingResult.hasFieldErrors()){
            List<FieldError> errors = bindingResult.getFieldErrors();
            return new ResponseMessage().setSuccess(false).setCode(100).setMessage("验证失败").add("errors",errors);
        }
        return articleService.add(article, multipartFile, request);
    }

    @GetMapping("/delete")
    @ResponseBody
    public boolean delete(String newsId){
        return articleService.deleteByPrimaryKey(newsId);
    }

    @GetMapping("/deleteSelected")
    @ResponseBody
    public boolean delete(String[] items){
        return articleService.deleteSelected(items);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseMessage update(@Validated Article article, BindingResult bindingResult,
                               @RequestParam(value = "file", required = false)MultipartFile multipartFile,
                               HttpServletRequest request) throws ParseException {
        if(bindingResult.hasFieldErrors()){
            List<FieldError> errors = bindingResult.getFieldErrors();
            return new ResponseMessage().setSuccess(false).setCode(100).setMessage("验证失败").add("errors",errors);
        }
        return articleService.update(article, multipartFile, request);
    }

    @GetMapping("/toUpdate")
    public String toUpdate(String newsId,boolean flag, Map<String, Object> map){
        Article updateArticle = articleService.selectOneByNewsId(newsId);
        map.put("updateArticle", updateArticle);
        if(flag == true){
            return "role/bind";
        }
        return "role/detail";
    }

}